from django.db import models
#from jrcmodel.models import MyConstants

STATUS_CHOICES = (
    (1, u"approved")
    , (2, u"deleted")
)

class JrcConfigCategory(models.Model):
    #status_id = models.SmallIntegerField(default = MyConstants.STATUS_APPROVED, choices = MyConstants.STATUS_CHOICES)
    status_id = models.SmallIntegerField(default = 1, choices = STATUS_CHOICES)
    name = models.CharField(max_length = 128)

    def __unicode__(self):
        return self.name

class JrcConfig(models.Model):
    #status_id = models.SmallIntegerField(default = MyConstants.STATUS_APPROVED, choices = MyConstants.STATUS_CHOICES)
    status_id = models.SmallIntegerField(default = 1, choices = STATUS_CHOICES)
    config_category = models.ForeignKey(JrcConfigCategory)
    name = models.CharField(max_length = 128)
    value = models.CharField(max_length = 256)

    def __unicode__(self):
        return self.name

    @staticmethod
    def get_value_by_name(name, default_value):
        val = default_value
        #config = JrcConfig.objects.filter(name = name).filter(status_id = MyConstants.STATUS_APPROVED).order_by("-id")[:1]
        try:
            config = JrcConfig.objects.filter(name = name).filter(status_id = 1).order_by("-id")[0]
            val = config.value
        except IndexError:
            val = default_value

        return val
